package fun.coding.leetcode;

import java.security.InvalidParameterException;
import java.util.List;

public class LeetCodeHelpers {
	
	/**
	 * Print out a two dimensional matrix
	 */
	public static void printMatrix(int[][] m) {
		if(m == null || m.length == 0 || m[0].length == 0) return;
		
		int row = m.length;
		int col = m[0].length;
		
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < col; j++) {
				System.out.print(m[i][j] + ", ");
			}
			System.out.println();
		}
	}
	
	/**
	 * print out a linked list 
	 */
	public static void printList(ListNode head) {
		ListNode iter = head;
		
		while (iter != null) {
			System.out.print(iter.val + ",");
			iter = iter.next;
		}
		System.out.println();
	}
	
	public static <T> void printGenericList(List<T> l) {
		for (T t : l) {
			System.out.print(t + ",");
		}
		System.out.println();
	}
	
	/**
	 * Build a linked list as in array[] {1, 2, 3} => 1->2->3
	 */
	public static ListNode buildLinkedList(int[] array) {
		if (array == null || array.length == 0) throw new InvalidParameterException("array cannot be null or empty");
		
		ListNode head = new ListNode(array[0]);
		ListNode it = head;
		
		for (int i = 1; i < array.length; i++) {
			ListNode t = new ListNode(array[i]);
			it.next = t;
			it = t;
		}
		
		return head;
	}
	
	/**
	 * Print out a list of list, takes generic type
	 */
	public static <T> void printListOfList(List<List<T>> list) {
		for (List<T> l : list) {
			for (T s : l) {
				System.out.print(s + ",");
			}
			System.out.println();
		}
	}
}
